{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "46d7ba64-6fa8-4be8-9464-a33862cc8e52",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[92mThis\u001b[0m\u001b[92m is a fantastic approach to\u001b[0m\u001b[92m building a domain knowledge DB for Huawei Router command-line actions! The YAML format is clean, human-readable, and machine-parsable, making it ideal for your intended use cases\u001b[0m\u001b[92m (vector embedding, display, and AI enrichment).\n",
      "\n",
      "Let's break down your current prompt and then discuss improvements.\n",
      "\n",
      "### Analysis of Your Current Prompt\n",
      "\n",
      "**\u001b[0m\u001b[92mStrengths:**\n",
      "\n",
      "1.  **Clear Goal:** You explicitly state you want a \"domain knowledge DB\" with \"common intents\" rather than single commands.\n",
      "2.  \u001b[0m\u001b[92m**Well-Defined Structure:** The YAML format with `description`, `synopsis`, `steps`, and `tags` is excellent.\n",
      "3.  **Good Example:** The provided example clearly illustrates the desired output and the level of detail.\u001b[0m\u001b[92m\n",
      "4.  **Inspiration Acknowledged:** Referencing PowerShell documentation is a good way to convey the desired style.\n",
      "5.  **Anticipated Use Cases:** Mentioning \"vector embedding\" and \"AI-enrichment\" helps the\u001b[0m\u001b[92m AI understand the purpose of each field.\n",
      "6.  **\"Less is more\" principle:** This is crucial for keeping the output concise and focused.\n",
      "\n",
      "**Areas for Improvement (Minor Tweaks & Clarifications):**\n",
      "\n",
      "1.  **Redundancy in \"Note\" vs. \"Prompt\":** Some\u001b[0m\u001b[92m information is repeated or could be streamlined.\n",
      "2.  **Clarity on \"AI-enrichment\" for tags:** While you mention it, explicitly stating *how* the AI should handle tags in the *extraction* phase could be beneficial.\n",
      "3.  **\"Existing test script\" conversion:** This is a separate\u001b[0m\u001b[92m task. While related, it might be better to address it as a subsequent prompt or a different phase. For the *extraction* prompt, focus solely on extraction.\n",
      "4.  **\"Array of item\" vs. \"list of items\":** \"List\" is slightly more common and unambiguous in this context.\n",
      "5\u001b[0m\u001b[92m.  **Handling edge cases/ambiguity:** What if a document describes multiple intents? What if an intent is very complex?\n",
      "\n",
      "### Improved Prompt Structure and Wording\n",
      "\n",
      "Here's a revised prompt that incorporates these improvements, aiming for maximum clarity and efficiency when interacting with an LLM:\n",
      "\n",
      "---\n",
      "\n",
      "**Prompt for Initial\u001b[0m\u001b[92m Extraction (from document to YAML list):**\n",
      "\n",
      "```\n",
      "You are an expert in Huawei Router command-line interfaces and documentation. Your task is to extract actionable knowledge from the provided text, focusing on \"common intents\" or sequences of commands that achieve a specific goal, rather than individual command definitions.\n",
      "\n",
      "Each extracted piece\u001b[0m\u001b[92m of knowledge should be represented as an item in a YAML list, following this exact structure:\n",
      "\n",
      "```yaml\n",
      "-\n",
      "  description: <A natural language explanation of the overall intent or purpose of this action. This should be concise but descriptive enough for vector embedding and understanding the goal.>\n",
      "  synopsis: <A brief\u001b[0m\u001b[92m, one-liner summary of the action, suitable for display in a list or UI.>\n",
      "  steps: <The detailed, step-by-step instructions to achieve the intent. This should primarily consist of command-line sequences, but can also include brief descriptions of necessary network environment setup or expected outcomes. Use plain text and\u001b[0m\u001b[92m preserve formatting (e.g., line breaks for commands).>\n",
      "  tags: <A list of relevant, single-word tags (e.g., ['system_configuration', 'interface_management', 'troubleshooting']). For this initial extraction, you should infer and generate these tags based on the content of the description and steps. Aim for 2-5 tags per entry.>\n",
      "```\n",
      "\n",
      "**Key Guidelines for Extraction:**\n",
      "\n",
      "*   **Focus on Intents:** Prioritize actions that achieve a specific, practical objective, often involving multiple commands. Avoid extracting single, isolated command definitions unless that command *itself* represents a complete, standalone\u001b[0m\u001b[92m intent.\n",
      "*   **Conciseness:** \"Less is more.\" Do not provide in-depth explanations of individual commands or comprehensive network documentation. Focus solely on the steps required to achieve the intent.\n",
      "*   **Directness:** Extract the information directly from the document. Do not invent commands or steps.\n",
      "*   **Format\u001b[0m\u001b[92m Adherence:** Strictly follow the YAML structure and field definitions.\n",
      "*   **Tag Generation:** Generate appropriate tags based on the content.\n",
      "\n",
      "**Example of Desired Output:**\n",
      "\n",
      "```yaml\n",
      "-\n",
      "  description: Entering the view of an interface varies with the physical attribute of the interface, allowing for configuration specific to that network\u001b[0m\u001b[92m port.\n",
      "  synopsis: Entering the Interface View.\n",
      "  steps: '\n",
      "1. Run `system-view` to enter the system view.\n",
      "2. Run `interface interface-type interface-number`: In this command, `interface-type` specifies the type of the interface (e.g., Gigabit\u001b[0m\u001b[92mEthernet, LoopBack), and `interface-number` specifies its unique identifier.\n",
      "3. (Optional) Run `commit` to save the configuration. Note: If the interface of the specified type and number already exists, `commit` might not be explicitly required after entering the view, but it's good practice for new\u001b[0m\u001b[92m configurations.'\n",
      "  tags: ['system_configuration', 'interface_management', 'cli_navigation']\n",
      "```\n",
      "\n",
      "Now, please extract the knowledge from the following document:\n",
      "\n",
      "---\n",
      "**Document to Process:**\n",
      "```\n",
      "<actual document content here>\n",
      "```\n",
      "---\n",
      "```\n",
      "\n",
      "### Explanation of Changes and Improvements:\n",
      "\n",
      "1.\u001b[0m\u001b[92m  **Clear Role Assignment:** \"You are an expert in Huawei Router command-line interfaces...\" sets the context well.\n",
      "2.  **Explicit \"Common Intents\" Emphasis:** Reinforces your primary goal.\n",
      "3.  **\"Exact Structure\" and \"Strictly Follow\":** Emphasizes the importance of the YAML format.\n",
      "\u001b[0m\u001b[92m4.  **Refined Field Descriptions:**\n",
      "    *   `description`: Added \"concise but descriptive enough for vector embedding and understanding the goal.\"\n",
      "    *   `steps`: Clarified \"primarily consist of command-line sequences,\" and \"preserve formatting.\" Also added \"brief descriptions of necessary network environment setup or expected outcomes\"\u001b[0m\u001b[92m to allow for more context within the steps if needed.\n",
      "    *   `tags`: Explicitly states \"For this initial extraction, you should infer and generate these tags based on the content...\" and \"Aim for 2-5 tags per entry.\" This guides the AI on *how* to generate tags during this phase\u001b[0m\u001b[92m.\n",
      "5.  **\"Key Guidelines for Extraction\" Section:** This is crucial. It consolidates your \"Notes\" and adds more specific instructions:\n",
      "    *   **Focus on Intents:** Re-emphasized.\n",
      "    *   **Conciseness:** Re-emphasized.\n",
      "    *   **Directness:**\u001b[0m\u001b[92m Prevents hallucination.\n",
      "    *   **Format Adherence:** Reinforces strictness.\n",
      "    *   **Tag Generation:** Provides clear instructions for this specific field.\n",
      "6.  **Improved Example:**\n",
      "    *   Added more detail to the `steps` to make it a better \"mini example\" (\u001b[0m\u001b[92me.g., \"to enter the system view,\" \"e.g., GigabitEthernet, LoopBack\").\n",
      "    *   Added a third tag (`cli_navigation`) to show variety.\n",
      "7.  **Clear Delimiters:** Using `---` before and after the document content clearly separates the prompt from the input\u001b[0m\u001b[92m.\n",
      "\n",
      "### Regarding Your Second Prompt Idea: \"existing test script\" -> \"Action[]\"\n",
      "\n",
      "This is a great *follow-up* task! I recommend keeping it separate.\n",
      "\n",
      "**How to approach the \"test script\" conversion:**\n",
      "\n",
      "You would create a *new* prompt, perhaps like this:\n",
      "\n",
      "```\n",
      "\u001b[0m\u001b[92mYou are an expert in Huawei Router command-line interfaces. Your task is to convert the provided Huawei Router test script into a structured YAML action item, following the same format as the knowledge base entries.\n",
      "\n",
      "Analyze the script to identify the primary intent or goal it aims to achieve. Then, extract the relevant commands and context\u001b[0m\u001b[92m into the 'steps' field.\n",
      "\n",
      "**Key Guidelines:**\n",
      "\n",
      "*   **Identify Primary Intent:** Determine the overarching goal of the script. This will form the `description` and `synopsis`.\n",
      "*   **Extract Steps:** Convert the script's commands and any accompanying explanations into the `steps` field.\u001b[0m\u001b[92m Ensure commands are correctly formatted (e.g., using backticks).\n",
      "*   **Generate Tags:** Infer relevant tags based on the script's actions.\n",
      "*   **Maintain Structure:** Adhere strictly to the YAML format.\n",
      "\n",
      "**YAML Format:**\n",
      "\n",
      "```yaml\n",
      "-\n",
      "  description: <natural language description of the intent the script achieves>\n",
      "  synopsis: <one-liner discription of the script's purpose>\n",
      "  steps: <the detailed command sequence from the script, with any necessary context>\n",
      "  tags: <generated tags>\n",
      "```\n",
      "\n",
      "**Example (if you had one):**\n",
      "\n",
      "```yaml\n",
      "\u001b[0m\u001b[92m-\n",
      "  description: The script configures a basic OSPF routing process on a router, including setting the router ID, defining areas, and specifying network interfaces for OSPF participation.\n",
      "  synopsis: Configure OSPF Routing.\n",
      "  steps: '\n",
      "# Test Script: Configure OSPF Basic\n",
      "system-view\n",
      "osp\u001b[0m\u001b[92mf 1 router-id 192.168.1.1\n",
      " area 0.0.0.0\n",
      "  network 10.0.0.0 0.0.0.255\n",
      "  network 10.0.1.0 0.0.0.\u001b[0m\u001b[92m255\n",
      " quit\n",
      "interface GigabitEthernet0/0/1\n",
      " ospf enable 1 area 0.0.0.0\n",
      " quit\n",
      "commit\n",
      "display ospf peer brief'\n",
      "  tags: ['routing', 'ospf', 'configuration', 'network_protocol']\n",
      "```\n",
      "\n",
      "**\u001b[0m\u001b[92mTest Script to Convert:**\n",
      "```\n",
      "<your test script here>\n",
      "```\n",
      "```\n",
      "\n",
      "By separating these two tasks, you make each prompt more focused and reduce the cognitive load on the LLM, leading to better and more consistent results.\n",
      "\n",
      "Your overall strategy is very sound, and with these prompt refinements, you should\u001b[0m\u001b[92m be able to build a robust and useful domain knowledge database!\u001b[0m"
     ]
    }
   ],
   "source": [
    "from u import Sess\n",
    "\n",
    "'''\n",
    "🦜: Let's try some different models\n",
    "'''\n",
    "\n",
    "from pathlib import Path\n",
    "import json\n",
    "\n",
    "k = json.loads(Path(\"/home/me/.ssh/k-250626-h3c.json\").read_text())\n",
    "s = Sess.new(model=\"google/gemini-2.5-flash\", url=k['url'], key=k['key'])\n",
    "#s = Sess.new(model=\"deepseek/deepseek-chat-v3-0324:free\", url=k['url'], key=k['key'])\n",
    "#s = Sess.new(model=\"x-ai/grok-code-fast-1\", url=k['url'], key=k['key'])\n",
    "\n",
    "\n",
    "\n",
    "s.add_q(Path('tmp.md').read_text()); await s.ask_for_a(); None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ac0b6294-0a0b-448b-84f8-57b71bade9a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You are an expert in Huawei Router command-line interfaces and documentation. Your task is to extract actionable knowledge from the provided text, focusing on \"common intents\" or sequences of commands that achieve a specific goal, rather than individual command definitions.\n",
      "\n",
      "Each extracted piece of knowledge should be represented as an item in a YAML list, following this exact structure:\n",
      "\n",
      "```yaml\n",
      "-\n",
      "  description: <A natural language explanation of the overall intent or purpose of this action. This should be concise but descriptive enough for vector embedding and understanding the goal.>\n",
      "  synopsis: <A brief, one-liner summary of the action, suitable for display in a list or UI.>\n",
      "  steps: <The detailed, step-by-step instructions to achieve the intent. This should primarily consist of command-line sequences, but can also include brief descriptions of necessary network environment setup or expected outcomes. Use plain text and preserve formatting (e.g., line breaks for commands).>\n",
      "  tags: <A list of relevant, single-word tags (e.g., ['system_configuration', 'interface_management', 'troubleshooting']). For this initial extraction, you should infer and generate these tags based on the content of the description and steps. Aim for 2-5 tags per entry.>\n",
      "```\n",
      "\n",
      "**Key Guidelines for Extraction:**\n",
      "\n",
      "*   **Focus on Intents:** Prioritize actions that achieve a specific, practical objective, often involving multiple commands. Avoid extracting single, isolated command definitions unless that command *itself* represents a complete, standalone intent.\n",
      "*   **Conciseness:** \"Less is more.\" Do not provide in-depth explanations of individual commands or comprehensive network documentation. Focus solely on the steps required to achieve the intent.\n",
      "*   **Directness:** Extract the information directly from the document. Do not invent commands or steps.\n",
      "*   **Format Adherence:** Strictly follow the YAML structure and field definitions.\n",
      "*   **Tag Generation:** Generate appropriate tags based on the content.\n",
      "\n",
      "**Example of Desired Output:**\n",
      "\n",
      "```yaml\n",
      "-\n",
      "  description: Entering the view of an interface varies with the physical attribute of the interface, allowing for configuration specific to that network port.\n",
      "  synopsis: Entering the Interface View.\n",
      "  steps: '\n",
      "1. Run `system-view` to enter the system view.\n",
      "2. Run `interface interface-type interface-number`: In this command, `interface-type` specifies the type of the interface (e.g., GigabitEthernet, LoopBack), and `interface-number` specifies its unique identifier.\n",
      "3. (Optional) Run `commit` to save the configuration. Note: If the interface of the specified type and number already exists, `commit` might not be explicitly required after entering the view, but it's good practice for new configurations.'\n",
      "  tags: ['system_configuration', 'interface_management', 'cli_navigation']\n",
      "```\n",
      "\n",
      "Now, please extract the knowledge from the following document:\n",
      "\n",
      "---\n",
      "**Document to Process:**\n",
      "```\n",
      "aaa\n",
      "```\n",
      "---\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# II stage 2. AW -> Cmd DB\n",
    "# --------------------------------------------------\n",
    "# 0. make prompt\n",
    "from pathlib import Path\n",
    "s = 'aaa'\n",
    "p = Path('p01-try-convert.md')\n",
    "p = p.read_text()\n",
    "p = p.replace('{{document_to_process}}', s)\n",
    "\n",
    "print(p)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c9510b81-e7f0-432b-8ab4-2d130a8ed1bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "!mkdir -p {p_qa}\n",
    "!echo '[]' > {p_qa/'index.json'}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4935b6c1-f85d-486b-b597-8fba6bfb478b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "📗️ : index Db saved to \u001b[92m/home/me/Documents/qa_db/index.json\u001b[0m,\n",
      " appended json = \n",
      "\u001b[94m{\n",
      "    \"model\": \"google/gemini-2.5-flash\",\n",
      "    \"url\": \"https://openrouter.ai/api/v1\",\n",
      "    \"note\": \"Year 1000: remember\"\n",
      "}\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "s.note = \"Year 1000: remember\"\n",
    "s.dump_to_db(p_qa)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
